;
$(function () {
    var ad = appData();
    var setting = {
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false
        },
        edit: {
            enable: true,
            editNameSelectAll: true,
            showRemoveBtn: showRemoveBtn,
            showRenameBtn: showRenameBtn,
            renameTitle: "编辑",
            removeTitle: "删除"
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeRemove: beforeRemove,
            beforeRename: beforeRename,
            onRename: onRename
        }
    };
    loadTree();

    function showRemoveBtn(treeId, treeNode) {
        return !treeNode.isParent;
    }

    function showRenameBtn(treeId, treeNode) {
        return !(treeNode.id == 0);
    }

    function beforeDrag(treeId, treeNodes) {
        return false;
    }


    function beforeRemove(treeId, treeNode) {
        _as.jConfirm("<p>确定要删除组织机构 " + treeNode.name + " 吗？</p>", function (obj) {
            ad.del("U_O_DELETE", {
                id: treeNode.id
            }, function () {
                loadTree();
                _as.jAlert("<p>组织机构删除成功</p>");
            })
        });
        return false;
    }

    function beforeRename(treeId, treeNode, newName, isCancel) {
        sessionStorage.oldNme = treeNode.name;
        if (newName.length == 0) {
            var zTree = $.fn.zTree.getZTreeObj("depart");
            zTree.cancelEditName();
            _as.jAlert("<p>组织机构名称不能为空</p>");
            return false;
        }
        return true;
    }
    function onRename(e, treeId, treeNode, isCancel) {
        if (!isCancel && sessionStorage.oldNme != treeNode.name) {
            if (treeNode.id) {
                ad.update("U_O_MODIFY", {
                    id: treeNode.id,
                    pId: treeNode.pId,
                    name: treeNode.name
                }, function () {
                    _as.jAlert("<p>组织机构修改成功</p>");
                })
            } else {
                ad.insert("U_O_ADD", {
                    pId: treeNode.pId,
                    name: treeNode.name
                }, function () {
                    _as.jAlert("<p>组织机构新建成功</p>");
                    loadTree();
                })
            }
        }
    }

    var newCount = 1;
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
            var zTree = $.fn.zTree.getZTreeObj("depart");
            treeNode = zTree.addNodes(treeNode, {
                pId: treeNode.id,
                name: ""
            });
            zTree.editName(treeNode[0]);

            return false;
        });
    };

    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };


    function loadTree() {
        ad.selectList("U_O_LIST", {}, function (data) {
            $.fn.zTree.init($("#depart"), setting, data);
        });
    }



});